<?php
/**
 * 用户收货地址模型
 * @author tangbl
 *
 */
class AddressModel extends Model {
	
	/*array(
		0.字段名   
		1.验证规则 [require 字段必须、email 邮箱、url URL地址、currency 货币、number 数字]
		2.提示信息  {%usr_form_validata_email_email}
		3.验证条件 [0.(默认)存在字段就验证;1.必须验证;2.值不为空的时候验证]
		4.附加规则 [regex、function、callback、confirm、equal、in、length、between、expire、ip_allow、ip_deny、unique]
		5.验证时   [1.新增数据时候验证;2.编辑数据时候验证;3.(默认)全部情况下验证]
	);*/
	protected $_validate=array(
		array('realname','require','{%cfg_action_ele_inputname}',1),
		array('mobile','require','{%cfg_action_ele_inputmobile1}',1),
		array('mobile','/^1\d{10}|\d{8}$/','{%cfg_action_ele_inputmobile2}',1), 
		//array('email','require','{%cfg_action_ele_inputemail1}',1), 
		//array('email','email','{%cfg_action_ele_inputemail2}',1),
	);
	
	//自动填充内容  addtime
	protected $_auto   =   array(
		array('addtime','time',1,'function'),
		array('solevar','create_id',1,'function'),
	);
	
	/**
	 * 获取用户的地址列表
	 * @param $userid int 用户编号
	 */
	public function getUserAddress($userid){
		
		$map['userid'] = $userid;
		return $this->where($map)->select();
	}
	
	/**
	 * 获取地址数据字典
	 */
	public function getAddressDict(){
		
		$lang    = getLang();
		$options = getDictByKey("Dict_address",$lang);
		return $options["array"];
	}
	
	/**
	 * 处理添加收货地址
	 * @param $model AddressModel 添加收货地址模型
	 * $model
	 */
	public function addAddress($model){
		
		if($model->isdefault){
			$arr['isdefault'] = 2;
			$map['isdefault'] = 1;
			$model->where($map)->save($arr);
		}
		
		$ssxadd = $model->ssxadd;
		$detail = $model->detail;
		$model->city    = I("param.area4","intval",0);
		$model->address = $ssxadd.$detail;
		$model->userid  = session(C('USER_AUTH_KEY'));
		
		return $model;
	}
	
	/**
	 * 处理修改收货地址
	 * @param $model AddressModel 修改收货地址模型
	 * $model
	 */
	public function editAdress($model){
		
		if($model->isdefault){
			$arr['isdefault'] = 2;
			$map['isdefault'] = 1;
			$model->where($map)->save($arr);
		}
		
		$ssxadd = $model->ssxadd;
		$detail = $model->detail;
		$model->city    = I("param.area4","intval",0);
		$model->address = $ssxadd.$detail;
		
		return $model;
	}
	
	/**
	 * 显示某个地区的地址详情
	 * @param $area int 数据字典地区编号
	 * return array
	 */
	public function showAreaDetail($area){
		
		$model = D("Dict");
		$row   = $model->find($area);
		
		$list  = mapByArray($area,"area");
		
		return array("dict"=>$row,"outlet"=>$list);
		
	}
	
	/**
	 * 处理订单配送地址
	 * @param $model Model 地址模型
	 */
	function orderAddress(){
		
		$addrid  = I("param.addrid",0,"intval");
			
		if(empty($addrid)){
			
			if($this->create()){
				$userid = session(C('USER_AUTH_KEY'));
				$data['realname'] = I("param.realname");
				$data['mobile']   = I("param.mobile");
				$data['email']    = I("param.email","","trim");
				$data['detail']   = I("param.detail","","trim");
				$data['ssxadd']   = I("param.ssxadd","","trim");
				$data['address']  = $data['ssxadd'].$data['detail'];
				$data['area']     = I("param.area",0,"intval");
				$data['city']     = I("param.area3",0,"intval");
				$data['outletid'] = I("param.outletid",0,"intval");
				$data['isdefault']= 2;
				$data['userid']   = $userid;
				$data['solevar']  = create_id();
				$data['addtime']  = time();
					
				$addrid = $this->add($data);
			}else{
				return array("id"=>0,"info"=>$this->getError());
			}
		}
		return array("id"=>$addrid);
	}
	
	//下订单时的地址处理
	public function order_address($user,$data){
		
		if($data['memaddress']){
			$model['tableid']  = $user['type'];
			$model['dataid']   = $user['id'];
			$model['mobile']   = $data['mobile'];
			$model['username'] = $data['realname'];
			$model['address']  = $data['ads'].$data['detail'];
			Log::write('MemberOrder:'.var_export($model,true),Log::ERR);

			//会员信息
			$member = M('Member')->find($user['id']);
			$model['remark'] = $member['remark1']; //客服备注内容
		}else{
			
			$rows['username'] = $data['email'];
			$member = M('Member')->where($rows)->find();
			
			if(!$member['id']){
				//先添加会员
				$map['isreg']    = 1;
				$map['solevar']  = create_id();
				$map['username'] = $data['email'];
				$map['email']    = $data['email'];
				$map['password'] = myencrypt($data['password']);
				$map['realname'] = $data['realname'];
				$map['address']  = $data['ads'].$data['detail'];
				$map['mobile']   = $data['mobile'];
				$map['addtime']  = time();
				$map['remark']   = $data['content'];
				$member['id'] = M('Member')->add($map);	
				Log::write('RegMemberSql:'.M('Member')->getLastSql().'|arr:'.var_export($map,true),Log::ERR);
			}else{
				$model['remark'] = $member['remark1'];//客服备注内容
			}

			//再添加地址
			$address = M('Address');
			$where['longitude'] = $data['longitude'];
			$where['latitude']  = $data['latitude'];
			$where['mobile']    = $data['mobile'];
			$where['userid']    = $member['id'];
			$where['city']      = $data['city'];
			$detail = $address->where($where)->find();
			Log::write('addressAddSql:'.$address->getLastSql().'|arr:'.var_export($where,true).'|arr:'.var_export($detail,true),Log::ERR);

			if(!$detail['id']){
				$mem['userid']    = $member['id'];
				$mem['tableid']   = 'Member';
				$mem['realname']  = $data['realname'];
				$mem['mobile']    = $data['mobile'];
				$mem['city']      = $data['city'];
				$mem['address']   = $data['ads'].$data['detail'];
				$mem['ads']       = $data['ads'];
				$mem['detail']    = $data['detail'];
				$mem['addtime']   = time();
				$mem['addressid'] = $data['addressid'];
				$mem['longitude'] = $data['longitude'];
				$mem['latitude']  = $data['latitude'];
				$detail = $mem;
				$address->add($mem);
				Log::write('NewAddressSql'.M('Address')->getLastSql().'|arr:'.'NewAddress:'.var_export($detail,true),Log::ERR);
			}

			$model['dataid']   = $member['id'];
			$model['tableid']  = 'Member';
			$model['mobile']   = $detail['mobile'];
			$model['username'] = $detail['realname'];
			$model['address']  = $detail['ads'].$data['detail'];

			//转移购物车
			$cat['dataid']  = $user['id'];
			$cat['tableid'] = $user['type'];
			$row['tableid'] = $model['tableid'];
			$row['dataid']  = $model['dataid'];
			M('Cart')->where($cat)->save($row);
			Log::write('MyCartSql:'.M('Cart')->getLastSql(),Log::ERR);
		}
		return $model;
	}
	
	//下订单时的地址处理
	public function order_address_bak($addressid){
		if($addressid){
			$addr['id'] = $addressid;
			$addrdetail = sendRest("RMS/Address/addrdetail",$addr);
		
			$model['mobile']    = $addrdetail['data']['mobile'];
			$model['username']  = $addrdetail['data']['realname'];
			$model['address']   = $addrdetail['data']['address'];
			$model['addressid'] = $addrdetail['data']['id'];
		}else{
			$model['mobile']    = I("mobile");
			$model['username']  = I("realname");
			$model['address']   = I("enaddress");
		}
		return $model;
	}
}
?>